package com.csxy.ljr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.ljr.entity.EduCourse;
import com.csxy.ljr.entity.EduCourseDescription;
import com.csxy.ljr.entity.vo.CourseInfoForm;
import com.csxy.ljr.entity.vo.CoursePublishVo;
import com.csxy.ljr.entity.vo.CourseQuery;
import com.csxy.ljr.entity.vo.CourseWebVo;
import com.csxy.ljr.exception.MyException;
import com.csxy.ljr.mapper.EduCourseMapper;
import com.csxy.ljr.mapper.EduCourseDescriptionMapper;
import com.csxy.ljr.service.EduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService { // 添加接口实现
    
    @Autowired
    private EduCourseDescriptionServiceImpl eduCourseDescriptionService;

    @Override
    // 添加课程信息
    public String addCourseInfo(CourseInfoForm courseInfoForm) {
//    向课程表里添加课程基本信息
//    把courseInfoForm对象转换成EduCourse对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm, eduCourse);
        int result = baseMapper.insert(eduCourse);

//        if (result == 0) {
//            // 插入失败处理（可抛出自定义异常）
//            // throw new GuliException(20001, "添加课程信息失败");
//            return null;
//        }
//

//    获取添加之后课程信息的ID
        String cid = eduCourse.getId();
//        向课程简介表里面添加课程简介

        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
//        手动设置描述课程表的ID，与上面的课程信息表ID关联
        eduCourseDescription.setId(cid);
        // 此处可以继续保存描述信息到对应表中（如有对应的mapper）

        return cid;
    }

    @Override
    public CourseInfoForm getCourseInfoById(String courseId) {
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        EduCourse eduCourse = baseMapper.selectById(courseId);
        BeanUtils.copyProperties(eduCourse, courseInfoForm);
        courseInfoForm.setDescription(eduCourseDescriptionService.getById(courseId).getDescription());
        return courseInfoForm;
    }

    @Override
    public void updateCourseInfo(CourseInfoForm courseInfoForm) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm, eduCourse);
        baseMapper.updateById(eduCourse);

        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        eduCourseDescription.setId(courseInfoForm.getId());
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }

    @Override
    public boolean removeCourseById(String id) {
        eduCourseDescriptionService.removeById(id);
        int result = baseMapper.deleteById(id);
        if (result>=1) {
            return true;
        }else {
            throw new MyException(20001, "删除失败");
        }
    }

    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        if (!title.isEmpty() ){
            queryWrapper.like("title",title);
        }
        if (!title.isEmpty()){
            queryWrapper.eq("status",status);
        }
        queryWrapper.orderByDesc("get_modified");
        baseMapper.selectPage(pageParam,queryWrapper);
    }
    @Autowired
    private EduCourseMapper eduCourseMapper;
    @Override
    public CoursePublishVo getPublishCourseInfo(String id) {
        return eduCourseMapper.getPublishCourseInfo(id);
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
    
    @Override
    public boolean publishCourse(String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal"); // 设置课程状态为已发布
        return this.updateById(eduCourse);
    }
}
